const fs = require('fs')
const request = require('request')
const rp = require('request-promise')
const host = 'http://www.bing.com'
const api = '/HPImageArchive.aspx?format=js&idx=0&n=1'
const path = 'C:\\Users\\stayfool\\Pictures\\bing\\'
const curr = new Date()
const file = path + curr.getFullYear() + '_' + (curr.getMonth() + 1) + '_' + curr.getDate() + '.jpg'

async function download() {
    let json = await rp.get({
        baseUrl: host,
        uri: api,
        json: true
    })

    await request.get(host + json.images[0].url).pipe(fs.createWriteStream(file))

    console.log('download ' + file + ' successed')
}

if (fs.existsSync(file)) {
    console.log('picture of today already downloaded')
} else {
    download()
}